package com.example.yls.cnbetaapp.httpService;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.example.yls.cnbetaapp.adapter.MessageAdapter;
import com.example.yls.cnbetaapp.amqp.RPCClient;
import com.example.yls.cnbetaapp.entity.MessageEntity;
import com.example.yls.cnbetaapp.util.GetBitmapUtil;
import com.example.yls.cnbetaapp.util.HttpUrlConnectionUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class MessageService {
    private ImageView imageView;
    private PicHandler pichdl;
    private List<MessageEntity> messageEntities;
    private String url;
    public  void setImage(ImageView imageView,String imageUrl){
        this.imageView = imageView;
        this.url = imageUrl;
        pichdl = new PicHandler();
        Thread t = new LoadPicThread();
        t.start();

    }
    class PicHandler extends Handler{

        @Override
        public void handleMessage(Message msg) {
            Bitmap myimg = (Bitmap) msg.obj;
            imageView.setImageBitmap(myimg);
        }
    }
    class LoadPicThread extends  Thread{
        @Override
        public void run(){
            Bitmap img = GetBitmapUtil.getBitmap(url);
            Message msg = Message.obtain();
            msg.what = 0;
            msg.obj = img;
            pichdl.sendMessage(msg);
        }
    }

        public void getMessageAdapter(Context context,int typeValue,Handler hl){
            MessageEntity messageEntity;
//            String messageUrl ="http://192.168.0.13:8080/getMessageJson?typeValue="+typeValue;
            messageEntities = new ArrayList<MessageEntity>();
            HttpUrlConnectionUtil httpUtil = new HttpUrlConnectionUtil();
//            String messageJson = httpUtil.getObjJson(messageUrl);
            RPCClient rpcClient = new RPCClient();
            String messageJson = rpcClient.getServerContent("message","messageQueue","messageExchange","messageReply",String.valueOf(typeValue));
            JSONArray jsonArray = null;
            try {
                jsonArray = new JSONArray(messageJson);
                for(int i =0;i<jsonArray.length();i++) {
                    messageEntity = new MessageEntity();
                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                    messageEntity.setImageUrl(jsonObject.getString("imageUrl"));
                    messageEntity.setTitle(jsonObject.getString("title"));
                    messageEntity.setIntro(jsonObject.getString("intro"));
                    messageEntity.setType(jsonObject.getString("type"));
                    messageEntity.setIssureTime(jsonObject.getString("issureTime"));
                    messageEntity.setReadingTimes(jsonObject.getString("readingTimes"));
                    messageEntity.setCommentCount(jsonObject.getString("commentCount"));
                    messageEntity.setMid(Integer.parseInt(jsonObject.getString("mid")));
                    messageEntities.add(messageEntity);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Message msg = Message.obtain();
            msg.what = 0;
            msg.obj =new  MessageAdapter(messageEntities,context);
            hl.sendMessage(msg);
        }
}
